/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejb.clients;

import cassys.application.services.AssessmentManagementBeanRemote;
import cassys.application.services.ClassGroupManagementBeanRemote;
import cassys.application.services.ModuleManagementBeanRemote;
import javax.naming.InitialContext;
import cassys.application.services.StudentManagementBeanRemote;
import cassys.application.services.UserManager;
import cassys.application.services.UserRoleManager;
import javax.naming.NamingException;

/**
 *
 * @author User
 */
public class ServiceLocator {

    private InitialContext initCtx;
    private static ServiceLocator serviceLocator;
    private ModuleManagementBeanRemote moduleManagementBeanRemote;
    private AssessmentManagementBeanRemote assessmentManagementBeanRemote;
    private ClassGroupManagementBeanRemote classGroupManagementBeanRemote;
    private StudentManagementBeanRemote studentManagementBeanRemote;
    private UserRoleManager loginBeanRemote;
    private UserManager userBeanRemote;

    static {
        serviceLocator = new ServiceLocator();
    }

    private ServiceLocator() {
        try {
            initCtx = new InitialContext();
        } catch (Exception ex) {
            System.out.println(ex.getStackTrace());
        }
    }

    public static ServiceLocator getInstance() {
        return serviceLocator;
    }

    public AssessmentManagementBeanRemote lookupAssessmentManagementBean() throws NamingException {
        if (assessmentManagementBeanRemote == null) {
            assessmentManagementBeanRemote = (AssessmentManagementBeanRemote) initCtx.lookup("AssessmentManagementBean");
        }

        return assessmentManagementBeanRemote;
    }

    public ModuleManagementBeanRemote lookupModuleManagementBean() throws NamingException {
        if (moduleManagementBeanRemote == null) {
            System.out.println("looking up the bean");
            moduleManagementBeanRemote = (ModuleManagementBeanRemote) initCtx.lookup("ModuleManagementBean");
            System.out.println("found the bean");

        }

        return moduleManagementBeanRemote;
    }

    public ClassGroupManagementBeanRemote lookupClassGroupManagementBean() throws NamingException {
        if (classGroupManagementBeanRemote == null) {
            classGroupManagementBeanRemote = (ClassGroupManagementBeanRemote) initCtx.lookup("ClassGroupManagementBean");
        }

        return classGroupManagementBeanRemote;
    }

    public StudentManagementBeanRemote lookupStudentManagementBean() throws NamingException {
        if (studentManagementBeanRemote == null) {
            studentManagementBeanRemote = (StudentManagementBeanRemote) initCtx.lookup("StudentManagementBean");
        }

        return studentManagementBeanRemote;
    }

    public UserRoleManager lookupLoginManagerBean() throws NamingException {
        if (loginBeanRemote == null) {
            loginBeanRemote = (UserRoleManager) initCtx.lookup("LoginController");
        }

        return loginBeanRemote;
    }

    public UserManager lookupUserManagerBean() throws NamingException {
        if (userBeanRemote == null) {
            userBeanRemote = (UserManager) initCtx.lookup("UserController");
        }

        return userBeanRemote;
    }
}
